package com.lezheba.action;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("rawtypes")
public class BaseAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	public static final String USER = "user";

	public String ajax(Object... objects) throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		for (Object object : objects) {
			out.print(object.toString());
		}

		return null;
	}


	public Map getSession() {
		return ActionContext.getContext().getSession();
	}

	public void putSession(Map<String, String> map) {
		for (Map.Entry<String, String> m : map.entrySet()) {
			putSession(m.getKey(), m.getValue());
		}

	}

	@SuppressWarnings("unchecked")
	public void putSession(String name, String value) {
		Map session = getSession();
		if (session != null) {
			session.put(name, value);
		}
	}

	public void removeSession(String name) {
		Map session = getSession();
		if (session != null) {
			session.remove(name);
		}
	}
	
	public String getSession(String name) {
		Map session = getSession();
		if (session != null) {
			return (String) session.get(name);
		}
		return null;
	}
	
	public String getEmail() {
		Map session = getSession();
		if (session != null) {
			return (String) session.get(USER);
		}
		return null;
	}

	public String execute() {
		return SUCCESS;
	}

}
